Borland Online And The Cobb Group Present:


January, 1995 - Vol. 2 No. 1

Highlighting your own keywords in the IDE editor

One of the more popular features of the Borland C++ 4.0 Integrated Development Environment (IDE) editor is syntax highlighting­­displaying important reserved words or language constructs in a special type style or color combination. However, the list of elements (parts of a source file) that you can customize from the Environment Options dialog box is somewhat limited.

In this article, we'll show how you can configure the IDE to allow you to customize the type style or color of certain words. Using this technique, you'll be able to quickly recognize class names, types, or special functions when they appear in your source code.

Token changes

By default, the IDE uses an internal list of reserved words and names whose appearance you can customize via syntax highlighting. However, you can tell the IDE to use a token file to highlight certain words. (The token file is the portion of source code that contains the reserved words your program requires to run properly.)

When you configure the IDE to use a token file, the IDE allows you to specify two types of entries: reserved words and client words. The reserved words consist of the C++ language reserved words and the Borland extensions to the language (things like _cdecl, _export, _pascal, and so on). All the reserved words will need to appear immediately after a section labeled [Reserved].

The client section is a portion of source code you can insert at the end of the token file. (We'll call this section [Client 1].) Here, you can add special words or labels that you want to highlight in a custom way. (You can create up to eight client sections.)

To begin using a token file, you'll open the Environment Options dialog box and specify the token file in the Reserved Words section of the Editor-File topic. To customize the appearance of the words in a client section, you'll move to the Syntax Highlighting-Customize topic and select the client section you want from the Element list box.

Once you've selected the appropriate client section, you can use the Colors palette to change the foreground or background color or the type style for those client words. When you close the Environment Options dialog box, the IDE will immediately use the custom highlighting settings for any client section words that appear in a source file.

Client surfing

Let's add some common Windows data types in a client section of one of the default token files that Borland provides. Begin by launching the Borland C++ 4.0 IDE.

When the IDE's main window appears, choose Open from the File menu. In the Open A File dialog box, enter the name \BC4\BIN\C.TOK in the File Name entry field and click OK.

In the editor window for the C.TOK file, scroll to the end of the file and enter

[Client 1]

Below the client label, enter BOOL and then enter HWND immediately below it, as shown in Figure A. Press [Return] after you've entered the last word. When you finish, choose Save from the File menu.


Figure A- You'll enter your own keywords in a client section at the end of the token file.

Now, choose Environment from the Options menu. After the Environment Options dialog box appears, click on the plus sign for the Editor item in the Topics list, then select the File subtopic. In the Reserved Words section of the dialog box, select the file C.TOK, as shown in Figure B.

Figure B - You use the Environment Options dialog box to configure the IDE to use a token file.

Next, click on the plus sign for the Syntax Highlighting item in the Topics list and select the Customize subtopic. In the Customize page, select Client 1 from the Element list box.

In the Color palette, click on the medium blue square with the left mouse button; then, select the Bold check box in the Attribute section, as shown in Figure C. Click OK to save these changes.

Figure C - You also set the color and type style settings for syntax highlighting in the Environment Options dialog box.

Now, choose Open from the File menu, enter \BC4\EXAMPLES\WINDOWS\ WHELLO\WHELLO.CPP in the File Name entry field, and click OK. When the editor window for the WHELLO.CPP file appears, use the vertical scroll bar to locate a function or command that contains BOOL or HWND, as shown in Figure D. To close the WHELLO.CPP file, double-click on the System menu icon.


Figure D - You can customize the syntax highlighting mechanism to enhance the appearance of important words or types.

Running changes

Contrary to the instructions in the IDE's online help, you can force the IDE to load or reload a token file without restarting the IDE. To do so, simply reopen the Environment Options dialog box, select the Syntax Highlighting Customize topic, and then select Internal in the Reserved Words list box. Click OK to save the new configuration. Then, reopen the Environment Options dialog box and reselect the C.TOK token file.

Conclusion

You can make several changes to the appearance of your source code by modifying the syntax highlighting settings. By highlighting your own keywords, you can make this feature even more useful.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.